package container

import (
	"chainmaker.org/chainmaker/smarteditor/util"
	"sync"
)

//BuildCache record build contract info of last time for every user
type BuildContractCache struct {
	cache sync.Map //map[string]string <uid,contract_names>
}

//AddBuildContractInfo add build contract info
func (bcc *BuildContractCache) AddBuildContractInfo(uid string, contractNames ...string) {
	contractNamesStr := util.ToNoRepeatString(contractNames...)
	bcc.cache.Store(uid, contractNamesStr)
}

//CleanBuildContractInfo clean build contract info from cache
func (bcc *BuildContractCache) CleanBuildContractInfo(uid string) {
	bcc.cache.Delete(uid)
}

//HasBuild if contract has build return true, else return false
func (bcc *BuildContractCache) HasBuild(uid string, contractNames ...string) bool {
	contractNamesStr := util.ToNoRepeatString(contractNames...)
	cacheContractNamesStr, ok := bcc.cache.Load(uid)
	return ok && contractNamesStr == cacheContractNamesStr.(string)
}
